
$(function(){
    $('#wh_theader a').on('click', function(){
        $('#wh_theader a').removeClass('selected');
        $(this).addClass('selected');
        changeTab();
    });

    $('#width').on('keyup', function(){
        var w = Number($(this).val());
        var h = w / returnRatio('w');
        $('#height').val(h.toFixed());
    }).focus(function(){
        $('.wh_w').addClass('spanBlur');
    }).blur(function(){
        $('.wh_w').removeClass('spanBlur');
    });

    $('#height').on('keyup', function(){
        var h = Number($(this).val());
        var w = h / returnRatio('h');
        $('#width').val(w.toFixed());
    }).focus(function(){
        $('.wh_h').addClass('spanBlur');
    }).blur(function(){
        $('.wh_h').removeClass('spanBlur');
    });

    $('#settings').on('click', function(){
        $('#settingWindow').css({display: 'block'});
        var obj = getCustomize();
        $('#firstNum').val(obj.num1);
        $('#secondNum').val(obj.num2);
    });

    $('#wh_cancel').on('click', function(){
        $('#settingWindow').css({display: 'none'});
    });

    $('#wh_ok').on('click', function(){
        //正数或者小数;
        var reg = /^\d+(\.\d+)?$/;
        var firstNum = $('#firstNum').val().trim();
        var secondNum = $('#secondNum').val().trim();
        if(!reg.test(firstNum)){
            $('#firstNum').focus();
            alert('宽度比必须输入数字');
            return;
        }
        if(!reg.test(secondNum)){
            $('#secondNum').focus();
            alert('高度比必须输入数字');
            return;
        }
        $('#customize').text(firstNum + ':' + secondNum);
        $('#settingWindow').css({display: 'none'});
        $('#wh_theader a').removeClass('selected');
        $('#wh_theader a:eq(5)').addClass('selected');
        changeTab();
    });

});

function changeTab(){
    var w = $('#width').val();
    if(/\d/.test(w)){
        w = Number(w);
        var h = w / returnRatio('w');
        $('#height').val(h.toFixed());
    }
}
function returnRatio(str){
    var index = $('#wh_theader a.selected').index();
    if(str == 'w'){
        switch(index){
            case 0:
                return 16/9;
                break;
            case 1:
                return 21/9;
                break;
            case 2:
                return 4/3;
                break;
            case 3:
                return 1.618/1;
                break;
            case 4:
                return 1.414/1;
                break;
            case 5:
                var obj = getCustomize();
                return obj.num1 / obj.num2;
                break;
        }
    }else if(str == 'h'){
        switch(index){
            case 0:
                return 9/16;
                break;
            case 1:
                return 9/21;
                break;
            case 2:
                return 3/4;
                break;
            case 3:
                return 1/1.618;
                break;
            case 4:
                return 1/1.414;
                break;
            case 5:
                var obj = getCustomize();
                return  obj.num2 / obj.num1;
        }
    }
}
function getCustomize(){
    var v = $('#customize').text();
    var arr = v.split(':');
    var v1 = Number(arr[0]);
    var v2 = Number(arr[1]);
    return {
        num1: v1,
        num2: v2
    };
}

